package org.taru.ems.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.taru.ems.admin.domain.ResidentInfo;
import org.taru.ems.admin.pojo.vo.QueryResidentInfoVo;

import org.taru.ems.common.common.CommonResult;

import java.util.List;

/**
 * (ResidentInfo)表控制层
 *
 * @author 涛哥
 * @since 2024-03-24 19:03:34
 */
@Api(tags = "居民信息前端控制器")
@RestController
@RequestMapping("/residentInfo")
public class ResidentInfoController {

    @Autowired
    private IService<ResidentInfo> iService;

    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @ApiOperation(value = "获取分页数据")
    @PostMapping("/getResidentInfosPage")
    public CommonResult getResidentInfosPage(@RequestBody QueryResidentInfoVo queryVo) {
        IPage<ResidentInfo> page = new Page(queryVo.getPageNum(), queryVo.getPageSize());
        LambdaQueryWrapper<ResidentInfo> queryWrapper = new QueryWrapper<ResidentInfo>().lambda();
        if(StringUtils.isNotBlank(queryVo.getName())){
            queryWrapper.eq(ResidentInfo::getName, queryVo.getName());
        }
        if(StringUtils.isNotBlank(queryVo.getIdCard())){
            queryWrapper.eq(ResidentInfo::getIdCard, queryVo.getIdCard());
        }
        if(StringUtils.isNotBlank(queryVo.getTelephone())){
            queryWrapper .eq(ResidentInfo::getTelephone, queryVo.getTelephone());
        }
        if(StringUtils.isNotBlank(queryVo.getRegion())){
            queryWrapper .eq(ResidentInfo::getRegion, queryVo.getRegion());
        }
        queryWrapper.orderByDesc(ResidentInfo::getCreateDate);
        iService.page(page, queryWrapper);
        return CommonResult.OK("获取分页成功", page);
    }


    /**
     * 根据id查询
     *
     * @param
     * @return
     */
    @ApiOperation(value = "获取居民信息明细")
    @GetMapping("/queryResidentInfoById/{id}")
    public CommonResult queryResidentInfoById(@PathVariable("id") String id) {
        ResidentInfo residentInfo = iService.getById(id);
        return CommonResult.OK("查询成功", residentInfo);
    }


    /**
     * 新增
     *
     * @param residentInfo
     * @return
     */
    @ApiOperation(value = "新增居民信息")
    @PostMapping("/addResidentInfo")
    public CommonResult addResidentInfo(@RequestBody ResidentInfo residentInfo) {
        iService.save(residentInfo);
        return CommonResult.OK("插入成功", residentInfo);
    }


    /**
     * 更新
     *
     * @param residentInfo
     * @return
     */
    @ApiOperation(value = "更新居民信息")
    @PutMapping("/updateResidentInfo")
    public CommonResult updateResidentInfo(@RequestBody ResidentInfo residentInfo) {
        iService.updateById(residentInfo);
        return CommonResult.OK("更新成功", residentInfo);
    }


    /**
     * 更新
     *
     * @param
     * @return
     */
    @ApiOperation(value = "删除居民信息")
    @DeleteMapping("/removeResidentInfo/{id}")
    public CommonResult removeResidentInfo(@PathVariable("id") String id) {
        iService.removeById(id);
        return CommonResult.OK("删除成功");
    }


}

